﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Configuration;

namespace WebSys.UI.Control.ZhengShou
{
    public class proupfujianControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        public string iid = string.Empty;
        string action = "add";
        Label fujianfolder;
        HtmlTable uptable, showmsg;
        CustomGridView GridList;
        Button upfile;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            iid = base.Page.Request["iid"];
            fujianfolder = (Label)FindControl("fujianfolder");
            uptable = (HtmlTable)FindControl("uptable");
            showmsg = (HtmlTable)FindControl("showmsg");
            GridList = (CustomGridView)FindControl("GridList");
            upfile = (Button)FindControl("upfile");
            upfile.Click += new EventHandler(upfile_Click);
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
            string strfoldername = base.Page.Request["foldername"];
            string straction = base.Page.Page.Request["action"];
            string strid = base.Page.Request["id"];
            string strrd = base.Page.Request["rd"];
            fujianfolder.Text = strfoldername;
            if (straction != null)
            {
                action = straction;
            }
            if (action == "del" && strid != null && iid != null && strfoldername != null)
            {
                _EluZSDataProvider.DeleteSome("zs_file", "and id='" + strid + "'");
                base.Page.Response.Redirect("proupfujian.aspx?iid=" + iid + "&foldername=" + fujianfolder.Text + "&rd=" + strrd);
            }
            GridList.OnLoadData += new EventHandler(GridList_OnLoadData);
        }
        protected void Initcy()
        {
            string strfoldername = base.Page.Request["foldername"];
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(strfoldername))
            {

                if (strfoldername == "下达补偿决定书的请示" ||
                   strfoldername == "派出所出具的身份证明和户籍资料" ||
                   strfoldername == "征收协商笔录"
                   )
                {
                    strWhere = " and type='" + strfoldername.Trim() + "' and ITEMID='" + iid + "'";
                    uptable.Visible = true;
                }
                else if (strfoldername == "建设用地批准书")
                {
                    strWhere = @" and itemname='土地权属证明文件' and ITEMID in (select pid from zs_zzbcjfenhu where id in (   
select 关联ID from xt_instance_ext where IID='" + iid + "'))";
                    GridList.Visible = true;
                }
                else if (strfoldername == "建设项目可行性研究报告的批复")
                {
                    strWhere = @" and itemname='项目批准文件' and ITEMID in (select pid from zs_zzbcjfenhu where id in (   
select 关联ID from xt_instance_ext where IID='" + iid + "'))";
                    GridList.Visible = true;
                }
                else if (strfoldername == "建设用地规划许可证")
                {
                    strWhere = @" and itemname='规划许可文件' and ITEMID in (select pid from zs_zzbcjfenhu where id in (   
select 关联ID from xt_instance_ext where IID='" + iid + "'))";
                    GridList.Visible = true;
                }
                else {
                    showmsg.Visible = false;
                    GridList.Visible = false;
                }
                string sql = "select * from zs_file where 1=1 " + strWhere;
                DataSet ds = _EluZSDataProvider.GetOneDs("(" + sql + ")", "*", "");
                GridList.DataSource = ds;
                GridList.RecordCount = ds.Tables[0].Rows.Count;
                GridList.DataKeyNames = new string[] { "ID" };
                GridList.DataBind();

                //  int isnotmodi = Convert.ToInt32(_EluZSDataProvider.GetOneStr("at_prolist", "ISNOTMODI", "Id", strPid));

                //if (isnotmodi == 1)
                //{
                //    uptable.Visible = false;
                //}
            }
            else
            {
                GridList.Visible = false;
                showmsg.Visible = true;
            }

        }
        protected void GridList_OnLoadData(object sender, EventArgs e)
        {
            Initcy();
        }
        protected void upfile_Click(object sender, EventArgs e)
        {
            string strfoldername = base.Page.Server.UrlDecode(base.Page.Request["foldername"]);
            FileUpload picFile = (FileUpload)FindControl("picFile");
            TextBox filename = (TextBox)FindControl("filename");
            HttpPostedFile postfile = picFile.PostedFile;
            string fileName = Path.GetFileName(postfile.FileName);
            string fileext = fileName.Substring(fileName.LastIndexOf("."));
            if ((fileext.ToLower() == ".jpg") || (fileext.ToLower() == ".gif") || (fileext.ToLower() == ".bmp") || (fileext.ToLower() == ".jpeg")
                || (fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx") || (fileext.ToLower() == ".png") || (fileext.ToLower() == ".xls") || (fileext.ToLower() == ".pdf"))
            {
                try
                {
                    if (!string.IsNullOrEmpty(postfile.FileName))
                    {
                        string filepath = ConfigurationManager.AppSettings["upfilepath"].ToString() + "proitems\\" + Guid.NewGuid().ToString().Replace("-", "") + "." + fileext;

                        string guidstr = Guid.NewGuid().ToString().Replace("-", "").ToUpper();

                        string filenametemp = filename.Text;
                        if (string.IsNullOrEmpty(filenametemp))
                        {
                            filenametemp = fileName.Replace(fileext, "");
                        }

                        _EluZSDataProvider.InsertSome("zs_file", "id,name,suffix,type,itemid", "'" + guidstr + "','" + filenametemp + "','" + fileext + "','" + strfoldername + "','" + iid + "'");

                        upfileblob(picFile, "zs_file", "FILECONTENT", guidstr);
                        Antu.Utilities.UtilitiesPage.AlertDialog("文件上传成功！", "window.parent.location.href=window.parent.location.href;");
                    }
                    else
                    {
                        Antu.Utilities.UtilitiesPage.AlertDialog("文件名不得为空！", "history.back();");
                    }
                }
                catch
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("上传出错！", "history.back();");
                }
            }
            else
            {
                Antu.Utilities.UtilitiesPage.AlertDialog("上传文件只支持jpg,gif,jpeg,bmp,doc,docx,xls,pdf,png格式！", "history.back();");
            }
        }
        private void upfileblob(FileUpload picFile, string tablename, string colname, string id)
        {
            try
            {
                HttpPostedFile postfile = picFile.PostedFile;
                if (!string.IsNullOrEmpty(postfile.FileName))
                {
                    Stream filestream = postfile.InputStream;
                    byte[] bFileContent = new byte[filestream.Length];

                    if (bFileContent.Length > 0)
                    {
                        filestream.Read(bFileContent, 0, bFileContent.Length);
                        try
                        {
                            _EluZSDataProvider.UpdateFileContent(tablename, colname, id, bFileContent);
                        }
                        catch
                        {
                            Antu.Utilities.UtilitiesPage.AlertDialog("上传出错！", "history.back();");
                        }
                    }

                }
                else
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("文件名不得为空！", "history.back();");
                }
            }
            catch
            {
                Antu.Utilities.UtilitiesPage.AlertDialog("上传出错！", "history.back();");
            }
        }

    }
}


